package com.hsj.demo.service;

import com.hsj.demo.dao.ChatHistoryDao;
import com.hsj.demo.dao.GroupDao;
import com.hsj.demo.dao.UserFriendDao;
import com.hsj.demo.entity.Group;
import com.hsj.demo.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
@Service
public class GroupService extends CurdService<GroupDao, Group> {
    @Autowired
    private GroupDao groupDao;
    @Autowired
    private UserFriendDao userFriendDao;

    public List<Group> findByUserId(Integer userId){
        return groupDao.findByUserId(userId);
    }
    public List<Group> findGroupNameByUserId(Integer userId){
        return groupDao.findGroupNameByUserId(userId);
    }
    public List<Group> findByKeyword(Group group,User user){
        group.setUserId(user.getId());
        return groupDao.findByKeyword(group);
    }
    public String insert(Group group, User user){
        List<Group> groupList = findGroupNameByUserId(user.getId());
        for(Group g: groupList){
            if(g.getGroupName()==group.getGroupName()){
                return "该分组已存在，请重新添加！";
            }
        }
        group.setUserId(user.getId());
        groupDao.insert(group);
        return "添加成功";
    }
    public String update(Group group, User user){
        List<Group> groupList = groupDao.findGroupNameByUserId(user.getId());
        for(Group g: groupList){
            if(g.getGroupName()==group.getGroupName()){
                return "该分组已存在，请重新修改！";
            }
        }
        group.setUserId(user.getId());
        groupDao.update(group);
        return "修改成功";
    }

    public String deleteByGroupId(Integer id) {
        if(userFriendDao.findByGroupId(id).size()>0){
            return "有属于该分组的好友未删除，不能删除该分组";
        }
        groupDao.deleteById(id);
        return "删除成功";
    }
}
